package cc.messcat.service.fupin;

import cc.messcat.common.model.Pager;
import cc.messcat.common.service.BaseServiceImpl;
import cc.messcat.dao.fupin.PreventPovertyDao;
import cc.messcat.entity.fupin.Family;
import cc.messcat.entity.fupin.PreventPoverty;
import cc.messcat.util.ExcelUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2018/5/4
 */
@Service
public class PreventPovertyServiceImpl extends BaseServiceImpl<PreventPoverty> implements PreventPovertyService {
    @Autowired
    private PreventPovertyDao preventPovertyDao;

    @Override
    public void complexMaintainPreventPoverty(String country, String village, String householderName, List<Family> preventPovertyFamilyList, List<Long> oldPreventFamilyID) {
        preventPovertyDao.maintainPreventPoverty(country,village,householderName,preventPovertyFamilyList,oldPreventFamilyID);
    }

    @Override
    public Pager<PreventPoverty> listPreventPoverty(PreventPoverty preventPoverty, Integer pageNo, Integer pageSize) {
        StringBuilder hql = new StringBuilder(" from PreventPoverty where 1=1 ");

        Map<String,Object> condition = new HashMap<>();
        if(StringUtils.isNotBlank(preventPoverty.getType())) {
            hql.append(" and type= :type ");
            condition.put("type", preventPoverty.getType());
        }
        if(StringUtils.isNotBlank(preventPoverty.getCountry())) {
            hql.append(" and country= :country ");
            condition.put("country", preventPoverty.getCountry());
        }
        if(StringUtils.isNotBlank(preventPoverty.getVillage())) {
            hql.append(" and village= :village ");
            condition.put("village", preventPoverty.getVillage());
        }
        if(StringUtils.isNotBlank(preventPoverty.getTarget())) {
            hql.append(" and target= :target ");
            condition.put("target", preventPoverty.getTarget());
        }

        return preventPovertyDao.getPagerByHql(pageNo, pageSize, hql.toString(),condition);

    }

    /**
     * 导出防贫预警信息
     */
    @Override
    public HSSFWorkbook exportPreventPoverty(PreventPoverty preventPoverty, Integer pageNo, Integer pageSize) {
        String[] cellName = new String[]
                {"序号","预警类型","所在乡","所在村","户主","防贫对象","金额","预警时间"};
        String[] field = new String[]
                {"type","country","village","householder","target","amount","warningTime"};
        Integer sheetCount = 0;
        Pager pager = null;
        Map<Integer,List> excelResult = new HashMap<>();

        do {
            sheetCount++;
            pager  = this.listPreventPoverty(preventPoverty,pageNo,pageSize);
            excelResult.put(sheetCount, pager.getResultList());

        } while(sheetCount < (pager.getRowCount()+4999) / 5000);

        HSSFWorkbook workbook = new HSSFWorkbook();
        for(int i = 1; i <= excelResult.size(); i++) {
            HSSFSheet sheet = workbook.createSheet("防贫预警信息表" + i);
            sheet.setDefaultColumnWidth(20);

            HSSFCellStyle headStyle =  ExcelUtil.getTitleStyle(workbook);
            HSSFCellStyle cellStyle =  ExcelUtil.getCellStyle(workbook);
            HSSFCellStyle columnTitleStyle =  ExcelUtil.getColumnTitleStyle(workbook);
            ExcelUtil.addTitle(sheet,cellName,"防贫预警信息表",headStyle,columnTitleStyle);
            ExcelUtil.addContextByList(sheet, excelResult.get(i), field, cellStyle, true);
        }
        return workbook;
    }

    @Override
    public List<Long> getFamilyIDByPoorFamilyID(Long id) {
        return preventPovertyDao.find(" select familyID from PreventPoverty where pfid="+id);
    }
}